package com.example.mypro;

import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import com.example.adapter.Tspub_LvAdapter;
import com.example.domain.TuShu;
import com.example.factory.ServiceFactory;
import com.example.service.TuShuService;

public class TspublicActivity extends Activity implements OnClickListener,
OnItemClickListener {
private ListView listView;
private ProgressBar pb;
private TextView tv;
private Button btn;
private Button search;
private List<TuShu> list = null;
private GetData getData;
private Intent intent;
private TuShuService tuShuService;
@Override
protected void onCreate(Bundle savedInstanceState) {
	tuShuService=ServiceFactory.getTuShuService();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tspublic);
listView = (ListView) findViewById(R.id.tspublic_lv);
pb = (ProgressBar) findViewById(R.id.tspublic_pb);
tv = (TextView) findViewById(R.id.tspublic_tv);
search=(Button)findViewById(R.id.searchbook);
btn = (Button) findViewById(R.id.tspublic_btn);
tv.setVisibility(View.INVISIBLE);
btn.setVisibility(View.INVISIBLE);
btn.setOnClickListener(this);
search.setOnClickListener(this);
listView.setVisibility(View.INVISIBLE);
listView.setOnItemClickListener(this);
pb.setVisibility(View.VISIBLE);
getData = new GetData();
getData.execute();

}

@SuppressLint("ShowToast")
class GetData extends AsyncTask<Void, Void, List<TuShu>> {

@SuppressLint("ShowToast")
@Override
protected List<TuShu> doInBackground(Void... params) {
	System.out.println("doInBackground");
	list = tuShuService.getTuShus();
	return list;
}

@Override
protected void onProgressUpdate(Void... values) {
	super.onProgressUpdate(values);

}

@Override
protected void onPostExecute(List<TuShu> result) {
	super.onPostExecute(result);
	pb.setVisibility(View.INVISIBLE);
	System.out.println("onProgressUpdate");
	if (list != null) {
		listView.setVisibility(View.VISIBLE);
		Tspub_LvAdapter adapter = new Tspub_LvAdapter(list,
				TspublicActivity.this);
		listView.setAdapter(adapter);
	} else {
		tv.setVisibility(View.VISIBLE);
		btn.setVisibility(View.VISIBLE);
	}
}

}

@Override
public void onClick(View v) {
if (v.getId() == R.id.tspublic_btn) {
	getData = new GetData();
	getData.execute();
}
if (v.getId() == R.id.searchbook) 
{
	intent = new Intent(TspublicActivity.this, SearchBookActivity.class);
	startActivity(intent);
	}
	}

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
	TuShu tuShu = list.get(arg2);
	intent = new Intent(TspublicActivity.this, TspublicActivityItem.class);
	intent.putExtra("Jy_sj", tuShu.getJy_sj());
	intent.putExtra("Ts_cbs", tuShu.getTs_cbs());
	intent.putExtra("Ts_id", tuShu.getTs_id());
	intent.putExtra("Ts_jj", tuShu.getTs_jj());
	intent.putExtra("Ts_sm", tuShu.getTs_sm());
	intent.putExtra("Ts_zz", tuShu.getTs_zz());
	startActivity(intent);
}

}
